<template>
	<view class="page">
		<view class="page-ctx">
			<view class="ctx-box">
				<view class="info-box">
					<view class="info-items">
						<view class="item">
							<view class="label">
								姓名
							</view>
							<view class="val">
								<view class="input-box">
									<input v-model="form.name" type="text" value="" placeholder="姓名" class="input"
										placeholder-class="input-p" />
								</view>
							</view>
						</view>
						<view class="item">
							<view class="label">
								手机号码
							</view>
							<view class="val">
								<view class="input-box">
									<input v-model="form.phone" maxlength="11" type="number" value="" placeholder="手机号码" class="input"
										placeholder-class="input-p" />
								</view>
							</view>
						</view>


						<view class="item" @click="openAddressSelect()">
							<view class="label">
								所在地区
							</view>
							<view class="val">
								<text class="text" v-if="provinceCityAreaText">{{provinceCityAreaText}}</text>
								<text class="text-p" v-else>请选择</text>
								<image class="arrow" src="@/static/common/arrow.png" mode="scaleToFill"></image>
							</view>
						</view>

						<view class="item">
							<view class="label">
								详细地址
							</view>
							<view class="val">
								<view class="input-box">
									<input v-model="form.address" type="text" value="" placeholder="详细地址" class="input"
										placeholder-class="input-p" />
								</view>
							</view>
						</view>

					</view>
				</view>


				<view class="switch-box flex-between">
					<view class="label">
						设为默认收货地址
					</view>
					<view class="val">
						<u-switch v-model="form.moren" :activeValue="1" :inactiveValue="0" size="20" activeColor="#CB0011"
							@change="changeSwitch"></u-switch>
					</view>
				</view>
			</view>
		</view>


		<w_bottom_safe height="140" />


		<view class="page-foot">
			<view class="safe-fixed-bottom">
				<view class="btns flex-between">
					<view v-if="form.id " class="btn flex-center btn-delete" @click="doDelete()">
						删除
					</view>
					<view class="btn flex-center btn-submit" @click="doSubmit()">
						保存
					</view>
				</view>
			</view>
		</view>


		<u-popup data-title="地址删除提示" :show="show_delete_pop" :overlay="true" mode="center" :closeable="false"
			:closeOnClickOverlay="true" @close="onClose" @open="onOpen" :safeAreaInsetBottom="true" round=16>
			<view class="cus-center-pop-box">
				<view class="pop-title">
					确定删除该地址吗？
				</view>
				<view class="pop-body">

				</view>
				<view class="pop-foot">
					<view class="btns flex-between">
						<view class="btn-qx flex-center" @click="show_delete_pop = false">
							取消
						</view>
						<view class="btn-qr flex-center" @click="confirmDoDelete()">
							确认
						</view>
					</view>
				</view>
			</view>
		</u-popup>

		<province_city_area_select_pop ref="province_city_area_select_pop" @confirm="confirmArea" />
		<province_city_area_select_pop2 ref="province_city_area_select_pop2" @cancel="handleCancel"
			@confirm="handleConfirm" />

	</view>
</template>

<script>
	import province_city_area_select_pop from '@/components/address/province_city_area_select_pop.vue'
	import province_city_area_select_pop2 from '@/components/address/province_city_area_select_pop2.vue'


	export default {
		components: {
			province_city_area_select_pop,
			province_city_area_select_pop2,
		},
		data() {
			return {
				//
				form: {
					name: "",
					phone: "",
					provinceCode: "",
					province: "",
					cityCode: "",
					city: "",
					areaCode: "",
					area: "",
					address: "",
					moren: 0,
					id: 0,
					longitude: '',
					latitude: '',
					shequId: '',
					addressType: 1,
				},

				show_delete_pop: false,
			}
		},
		computed: {
			provinceCityAreaText() {
				let text = ''
				if (this.form.area) {
					text = [this.form.province, this.form.city, this.form.area].join('-')
				}

				return text
			}
		},
		onLoad(option) {
			this.id = option.id || ''
			if (this.id) {
				uni.setNavigationBarTitle({
					title: '编辑地址'
				})
				this.setView();
			} else {
				uni.setNavigationBarTitle({
					title: '新增地址'
				})
			}
		},
		onShow() {

		},
		methods: {
			setView() {
				this.showLoading()
				this.$api({
					url: '/service.php',
					method: 'get',
					data: {
						action: 'userAddress_detail',
						id: this.id
					},
				}).then(res => {
					if (res.code == 200) {
						let data = res.data;
						this.form = {
							name: data.name,
							phone: data.phone,
							provinceCode: data.provinceCode,
							province: data.province,
							cityCode: data.cityCode,
							city: data.city,
							areaCode: data.areaCode,
							area: data.area,
							address: data.address,
							moren: data.moren,
							id: data.id,
							longitude: data.longitude,
							latitude: data.latitude,
							shequId: data.shequId,
							addressType: data.addressType,
						}
					}

					this.hideLoading()
				})
			},
			changeSwitch(e) {
				// console.log(e)
				// this.form.moren = e ? 1 : 0
			},

			openAddressSelect() {
				this.$refs.province_city_area_select_pop.init(this.form)
				// this.$refs.province_city_area_select_pop2.init()
			},

			handleCancel() {

			},
			handleConfirm(option) {
				this.$log('option', option)
				let idList = option.id;
				let nameList = option.name;

				this.form.province = nameList[0];
				this.form.city = nameList[1];
				this.form.area = nameList[2];

				this.form.provinceCode = idList[0];
				this.form.cityCode = idList[1];
				this.form.areaCode = idList[2];
			},


			confirmArea(option) {
				this.$log('省市区选择', option)
				let {
					prov,
					city,
					area
				} = option

				this.form.province = prov.name;
				this.form.city = city.name;
				this.form.area = area.name;

				this.form.provinceCode = prov.code;
				this.form.cityCode = city.code;
				this.form.areaCode = area.code;
			},


			doSubmit() {
				if (!this.form.name) {
					this.$alert('请填写姓名')
					return
				}
				if (!this.form.phone) {
					this.$alert('请填写手机号码')
					return
				}
				if (!this.form.areaCode) {
					this.$alert('请选择所在地区')
					return
				}
				if (!this.form.address) {
					this.$alert('请填写详细地址')
					return
				}

				//节流
				//2秒内最多只触发一次 
				uni.$u.throttle(this.doSubmitFn, 2000, true)
				//防抖
				//连续操作后 只要最后一次操作2秒内没有操作 则执行方法
				// uni.$u.debounce(this.doSubmitFn, 2000, false)
			},

			doSubmitFn() {
				this.$api({
					url: '/service.php',
					method: 'get',
					data: {
						action: 'userAddress_add',
						...this.form,
					},
				}).then(res => {
					this.$alert(res)
					if (res.code == 200) {
						this.toBack({
							time: 1500
						})
					}
				})
			},

			doDelete() {
				// this.$refs.address_delete_pop.init()
				this.show_delete_pop = true;
			},

			confirmDoDelete() {
				uni.$u.throttle(this.doDeleteFn, 2000, true)
			},

			doDeleteFn() {
				this.$api({
					url: '/service.php',
					method: 'get',
					data: {
						action: 'userAddress_delete',
						id: this.form.id
					},
				}).then(res => {
					this.$alert(res)
					if (res.code == 200) {
						this.removeCache

						this.toBack({
							time: 1500
						})
					}
				})
			},


			onClose() {
				this.show_delete_pop = false;
			},
			onOpen() {},


		}
	}
</script>

<style lang="less">
	page {

		background: #F8F8F8;

	}
</style>

<style lang="less">
	.page {}

	.page-ctx {
		padding: 24rpx 32rpx;
	}


	.info-box {
		margin-bottom: 16rpx;
		background: #FFFFFF;
		border-radius: 8rpx 8rpx 8rpx 8rpx;
		padding: 24rpx 24rpx;
		// overflow: hidden;

		background: #FFFFFF;
		// box-shadow: 0rpx 0rpx 16rpx 2rpx rgba(0, 0, 0, 0.08);
		border-radius: 8rpx 8rpx 8rpx 8rpx;
	}

	.info-items {
		.item {
			display: flex;
			align-items: center;
			justify-content: space-between;
			min-height: 108rpx;

			&+.item {
				border-top: 2rpx solid #F5F5F5;
			}

			.label {
				font-family: PingFang SC, PingFang SC;
				font-weight: 400;
				font-size: 28rpx;
				color: #000000;
			}

			.val {
				padding-left: 40rpx;
				flex: 1;
				overflow: hidden;
				display: flex;
				justify-content: flex-end;
				align-items: center;


				font-family: PingFang SC, PingFang SC;
				font-weight: 400;
				font-size: 28rpx;
				color: #000000;

				.input-box {
					width: 100%;
				}

				.text {
					font-family: PingFang SC, PingFang SC;
					font-weight: 400;
					font-size: 28rpx;
					color: #000000;
				}

				.text-p {
					font-family: PingFang SC, PingFang SC;
					font-weight: 400;
					font-size: 28rpx;
					color: #AAAAAA;
				}

				.arrow {
					margin-left: 30rpx;
					width: 12rpx;
					height: 20rpx;
				}

				.input {
					text-align: right;
					font-size: 28rpx;
				}

				.input-p {
					text-align: right;
					font-size: 28rpx;
					color: #AAAAAA;
				}

			}
		}
	}


	.switch-box {
		padding: 20rpx 20rpx;

		.label {
			font-family: PingFang SC, PingFang SC;
			font-weight: 400;
			font-size: 28rpx;
			color: #000000;
		}

		.val {}
	}

	// 

	.delete-box {
		margin-top: 80rpx;
		padding: 40rpx;

		.btn {
			text-align: center;
			background: #ffffff;
			width: 100%;
			height: 70rpx;
			line-height: 70rpx;
			font-size: 28rpx;
			color: #ff0000;
			border: 1rpx solid #CCCCCC;
			border-radius: 40rpx;
		}
	}


	.page-foot {

		.btns {
			width: 100%;

			.btn {

				// width: 100%;
				height: 80rpx;
				border-radius: 40rpx;
				background: #CB0011;

				font-family: PingFang SC, PingFang SC;
				font-weight: 600;
				font-size: 28rpx;
				color: #FFFFFF;

				&+.btn {
					margin-left: 24rpx;
				}


				&.btn-delete {
					width: 176rpx;
					height: 80rpx;
					background: #F8F8F8;
					border-radius: 40rpx 40rpx 40rpx 40rpx;

					font-family: PingFang SC, PingFang SC;
					font-weight: 400;
					font-size: 28rpx;
					color: #000000;
				}

				&.btn-submit {
					flex: 1;
				}


				&.btn-disabled {
					background: #DDDDDD;
				}

				&[disabled] {
					background: #DDDDDD;
				}
			}
		}
	}




	// 删除弹窗

	.cus-center-pop-box {
		padding: 24rpx;
		padding-top: 60rpx;
		padding-bottom: 60rpx;
		width: 686rpx;
		min-height: 300rpx;
		background: #FFFFFF;
		border-radius: 32rpx;

		.pop-title {

			text-align: center;
			font-weight: bold;
			font-size: 28rpx;
			color: #000000;
		}


		.pop-body {
			margin-top: 30rpx;
			padding: 0 60rpx;

			.tip {
				font-family: PingFang SC, PingFang SC;
				font-weight: 400;
				font-size: 24rpx;
				color: #000000;
				line-height: 1.6;
				text-align: center;

				.tip-text {

					font-family: PingFang SC, PingFang SC;
					font-weight: 400;
					font-size: 24rpx;
					color: #000000;
					line-height: 1.6;
					text-align: center;

					&+.tip-text {
						margin-top: 20rpx;
					}
				}
			}

		}


		.pop-foot {
			margin-top: 60rpx;

			.btns {

				.btn-qx {
					width: 306rpx;
					height: 80rpx;
					background: #F8F8F8;
					border-radius: 40rpx 40rpx 40rpx 40rpx;
					font-weight: 400;
					font-size: 28rpx;
					color: #000000;
				}

				.btn-qr {
					width: 306rpx;
					height: 80rpx;
					background: #CB0011;
					border-radius: 40rpx 40rpx 40rpx 40rpx;
					font-weight: 400;
					font-size: 28rpx;
					color: #FFFFFF;

				}
			}

		}
	}
</style>